<?php 
 header("Content-type: text/html; charset=utf-8");     
include('../conn.php');
error_reporting(0);
	$resultArray = array();

	//商品id
	$goodsid = '';
	//购买数量
	$addnum = '';
	//用户id
	$userid = '';

	$total = '';

	$shopid = '';
	//收货地址id
	$addressid = '';
	if(isset($_GET['addressid'])){
		$addressid = $_GET['addressid'];
	}

	if(empty($_GET['goodsid'])){
		$resultArray['code'] = -100;
		$resultArray['msg'] = 'goodsid不能为空';

		echo json_encode($resultArray);
		exit;
	}else{
		$goodsid = $_GET['goodsid'];
	}

	if(empty($_GET['addnum'])){
		$resultArray['code'] = -100;
		$resultArray['msg'] = 'addnum不能为空';
		echo json_encode($resultArray);
		exit;
	}else{
		$addnum = $_GET['addnum'];
	}

	if(empty($_GET['userid'])){
		$resultArray['code'] = -100;
		$resultArray['msg'] = 'userid不能为空';
		echo json_encode($resultArray);
		exit;
	}else{
		$userid = $_GET['userid'];
	}
	//查找商品运费信息
	$query_muid = mysql_query("select goodsid,muid,zixilie,shopid from goods where goodsid = ".$goodsid);
	if(!$query_muid){
		$resultArray['code'] = -100;
		$resultArray['msg'] = '商品信息异常';
		echo json_encode($resultArray);
		exit;

	}
	
	// 查询默认地址 
	$address_info = array();
	
	// 查找送货地址
	if ($addressid != '') {

		$query = mysql_query("select * from address where id=".$addressid);
	}else{
		// 默认地址
		$query = mysql_query("select * from address where userid=".$userid." and state=1");
		// 默认地址 end
	}
	$myaddress = mysql_fetch_assoc($query);
	if($myaddress){
		$address_info['code'] = 200;
		$address_info['data'] = $myaddress;
		$address_info['msg'] = '查询到默认收货地址';
	}else{
		$address_info['code'] = -100;
		$address_info['msg'] = '没有查询到默认收货地址';
	}
            

	
	// 计算运费
	$fare_total = 0;
	$allFreight = 0;
	
	$row_muid = mysql_fetch_assoc($query_muid);
	if(!$row_muid){
		$resultArray['code'] = -100;
		$resultArray['msg'] = '商品信息异常';
		echo json_encode($resultArray);
		exit;
	}
	//获取shopid
	if($shopid == ''){
		$shopid = $row_muid['shopid'];
	}
	
	$sql = mysql_query("select type from zifenlei where id=".$row_muid['zixilie']);
	$type = mysql_fetch_assoc($sql);
	$type = $type['type'];
	if ($type == 1) { // 快购
		$query = mysql_query("select distribution_fee from shop where shopid=".$row_muid['shopid']);
		$distribution_fee = mysql_fetch_assoc($query);
		$distribution_fee = $distribution_fee['distribution_fee'];
		$fare_total += $distribution_fee;
	}else{ // 海淘
		// 运费条件
		
		$sql = mysql_query("select * from fare where `area` like '%".$myaddress[quyu]."%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
	
		$result0 = mysql_fetch_assoc($sql);

		$sql = mysql_query("select * from fare where `area` like '%".$myaddress[city]."%'  AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
		$result1 = mysql_fetch_assoc($sql);

		$sql = mysql_query("select * from fare where `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
		$result2 = mysql_fetch_assoc($sql);


		$sql = mysql_query("select * from fare where `area` like '%0%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
		$result3 = mysql_fetch_assoc($sql);

		$sql = mysql_query("select * from fare where `area` like '%".$myaddress[quyu]."%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = 0 and isPay = 0)");
		$result4 = mysql_fetch_assoc($sql);

		$sql = mysql_query("select * from fare where `area` like '%".$myaddress[city]."%'  AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = 0 and isPay = 0)");
		$result5 = mysql_fetch_assoc($sql);

		$sql = mysql_query("select * from fare where `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = 0 and isPay = 0)");
		$result6 = mysql_fetch_assoc($sql);

		$sql = mysql_query("select * from fare where `area` like '%0%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = 0 and isPay = 0)");
		$result7 = mysql_fetch_assoc($sql);
		
		if ($result0) {
			$where_area = "AND `area` like '%".$myaddress[quyu]."%' and muid in (select id from fareml where shopid = ".$shopid.")";
		}elseif ($result1) {
			$where_area = "AND `area` like '%".$myaddress[city]."%' AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid in (select id from fareml where shopid = ".$shopid.")";
		}elseif ($result2) {
			$where_area = "AND `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid in (select id from fareml where shopid = ".$shopid.")";
		}elseif ($result3){
			$where_area = "AND `area` like '%0%' and muid in (select id from fareml where shopid = ".$shopid.")";
		}elseif ($result4) {
			$where_area = "AND `area` like '%".$myaddress[quyu]."%' and muid in (select id from fareml where shopid = 0)";
		}elseif ($result5) {
			$where_area = "AND `area` like '%".$myaddress[city]."%' AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid in (select id from fareml where shopid = 0)";
		}elseif ($result6) {
			$where_area = "AND `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid in (select id from fareml where shopid = 0)";
		}else{
			$where_area = "AND `area` like '%0%' and muid in (select id from fareml where shopid = 0)";
		}

		// 运费条件 end

		// 计算运费 
		$sql = mysql_query("select * from fare  WHERE muid = ".$row_muid['muid']."  ".$where_area);
		$fare_row  = mysql_fetch_assoc($sql);
		if($fare_row){
			$fare = $fare_row['fare'];
			$addfare = $fare_row['addfare'];
			$begincount = $fare_row['begincount'];
			$distance_count = $fare_row['distance_count'];

			//加上初始件数运费
			
			$fare+=0;
			$begincount+=0;
			$distance_count+=0;
			$fare_total += $fare;
			//购买的件数大于初始件数

			if($addnum > $begincount){
				//计算超过件数
				$distance_num = $addnum - $begincount;
				//计算需要加多少次，向下取整
				$addfare_count = floor($distance_num/$distance_count);

				$fare_total += $addfare_count*$addfare;

			}


		}
		
		// 计算运费 end

		// // 计算运费险
		$query = mysql_query("select * from freightrisk");
		$freightRisk = 0;
		while ($row = mysql_fetch_assoc($query)) {
			if ($addnum >= $row[goodsNum]) {
				$freightRisk = $row[freightRisk];
			}
		}
		$allFreight += $freightRisk;
		// // 计算运费险 end
	}


	// 商家信息 
	$query = mysql_query("select freight_ins,name,shopid from shop where shopid=".$shopid);
	$shop_result = mysql_fetch_assoc($query);
	$shopInfo = array();
	if($shop_result){

		$shop_result['freightRisk'] = $allFreight;
		$shop_result['distribution_fee'] = $fare_total;

		$shopInfo['code'] = 200;
		$shopInfo['data'] = array($shop_result);
		$shopInfo['msg'] = '获取商家信息成功';

	}else{
		$resultArray['code'] = -100;
		$resultArray['msg'] = '商家信息异常';
		echo json_encode($resultArray);
		exit;
	}
	$resultArray['code'] = 200;
	$resultArray['msg'] = '获取成功';
	$resultArray['address_info'] = $address_info;
	$resultArray['shopInfo'] = $shopInfo;
	echo json_encode($resultArray);

	



 ?>